package com.example.dmz

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.android.FlutterFragmentActivity
import java.util.*
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.Result
import androidx.core.content.ContextCompat
import androidx.core.app.ActivityCompat
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterFragmentActivity(){

    //这个跟flutter的通道名称保持一直
    private lateinit var mResult: Result
    private var hashMap: HashMap<String, String> = HashMap<String,String>()

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine)
        MethodChannel(flutterEngine?.dartExecutor!!,Utils.CHANNEL_NAME).setMethodCallHandler { call, result ->
            mResult = result;
            //这里是判断是不是调getBetteryLevel方法，因为所有的方法都走这里
            if(call.method == Utils.METHOD_REQUEST_PERMISSION){
                getPermission()
            } else{
                result.notImplemented()
            }
        }
    }

    fun getPermission() {
        val permissionList: MutableList<String> = ArrayList()
        for (permission in arrayOf(
            Manifest.permission.BLUETOOTH_CONNECT,
            Manifest.permission.BLUETOOTH_SCAN,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CALL_PHONE,
            Manifest.permission.USE_BIOMETRIC
        )) {
            if (ContextCompat.checkSelfPermission(
                    this,
                    permission
                ) !== PackageManager.PERMISSION_GRANTED
            ) {
                permissionList.add(permission)
            }
        }
        if (!permissionList.isEmpty()) {
            ActivityCompat.requestPermissions(this, permissionList.toTypedArray(), 1)
        }
    }
}

